'use strict';
import { Application } from 'egg';
import * as moment from 'moment';
import * as pageFindPlugin from 'mongoose-paginate';
export default (app: Application) => {
    const mongoose = app.mongoose;
    // const ObjectId = mongoose.Types.ObjectId;
    const schema = new mongoose.Schema({
        ctime: { type: String , default: moment().format('YYYY-MM-DD HH:mm:ss'), required: true },
        name: { type: String, required: true },
        phone: { type: String, required: true },
        excom: { type: String, ref: 'Expresscompany' },
        sort: { type: Number, default: 0 },
        excooperation: { type: Boolean, required: true, default: false },
        site: {
            type: String, required: true, ref: 'Site',
        },
        desc: { type: String },
        money: {
            type: Number,
            default: 0 ,
            get: (val: string) => Math.round(parseFloat(val)) / 100,
            set: (val: number) => val * 100,
        },
        price: {
            type: Number,
            required: true,
            default: 30,
            get: (val: number) => val / 100,
            set: (val: number) => val * 100,
        },
        status: { type: Number, default: 0 }, // 0正常 1禁用 9删除
        logs: { type: Array },
        sale: { type: [{
                region: Array,
                fprice: Number,
                sprice: Number,
                fweight: Number,
            }], default: [],
        },
    }, {
        id: false,
        toJSON: {
            getters: true,
            virtuals: true,
        },
        timestamps: {
            updatedAt: true,
        },
    });
    schema.plugin(pageFindPlugin, {
        id: false,
        toJSON: {
            getters: true,
            virtuals: true,
        },
    });
    schema.index({
        site: 1,
        name: 1,
        phone: 1,
        excom: 1,
        sort: -1,
        status: 1,
    });
    return mongoose.model('Partner', schema, 'partner');
};
